import 'package:flutter/material.dart';
import '../../../../widget/paginationControls.dart';
import '../../../../services/screenAdapter.dart';

import 'package:get/get.dart';

import '../controllers/project_user_controller.dart';

class ProjectUserView extends GetView {
  final int projectId;
  ProjectUserView({super.key, required this.projectId});
  final ProjectUserController controller = Get.put(ProjectUserController());

  Widget _buildTableHeader() {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 12, horizontal: 8),
      decoration: BoxDecoration(
        color: Colors.grey[200],
        borderRadius: BorderRadius.circular(8),
      ),
      child: Row(
        children: [
          _buildHeaderCell('用户名称', flex: 2),
          _buildHeaderCell('用户电话', flex: 2),
          _buildHeaderCell('操作', flex: 6),
        ],
      ),
    );
  }

  Widget _buildHeaderCell(String text, {int flex = 1}) {
    return Expanded(
      flex: flex,
      child: Text(text, style: TextStyle(fontWeight: FontWeight.bold)),
    );
  }

  Widget _buildUserItemRow(item) {
    return Container(
      margin: EdgeInsets.only(bottom: 8),
      padding: EdgeInsets.symmetric(vertical: 12, horizontal: 8),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(color: Colors.black12, blurRadius: 2, offset: Offset(0, 1)),
        ],
      ),
      child: Row(
        children: [
          Expanded(
            flex: 2,
            child: Text(item.name, overflow: TextOverflow.ellipsis),
          ),
          Expanded(
            flex: 2,
            child: Text(item.phone, overflow: TextOverflow.ellipsis),
          ),
          Expanded(
            flex: 6,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                ElevatedButton(
                  onPressed: () {
                    // 实现删除接口逻辑
                    // controller.deleteInterface(item.id ?? 0);
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.red,
                    foregroundColor: Colors.white,
                    padding: const EdgeInsets.symmetric(
                      horizontal: 16,
                      vertical: 8,
                    ),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(4),
                    ),
                  ),
                  child: const Text("删除", style: TextStyle(fontSize: 12)),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    controller.setProjectId(projectId);
    return Scaffold(
      body: Container(
        padding: EdgeInsets.all(ScreenAdapter.width(10)),
        color: Colors.white,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 分类标题 + 添加接口按钮
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Obx(() {
                  return Text(
                    "共 ${controller.totalUser} 个用户",
                    style: const TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                    ),
                  );
                }),
                ElevatedButton(
                  onPressed: () {
                    // 实现添加接口逻辑，比如弹出表单对话框
                    // Get.dialog(CreateInterface());
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.blue,
                    foregroundColor: Colors.white,
                    padding: const EdgeInsets.symmetric(
                      horizontal: 16,
                      vertical: 8,
                    ),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(4),
                    ),
                  ),
                  child: const Text("添加接口", style: TextStyle(fontSize: 12)),
                ),
              ],
            ),
            SizedBox(height: ScreenAdapter.height(16)),
            // 表头
            _buildTableHeader(),
            SizedBox(height: ScreenAdapter.height(8)),
            Obx(() {
              return controller.userList.isNotEmpty
                  ? Expanded(
                      child: ListView.builder(
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        itemCount:
                            controller.userList.length + 1, // +1 用于底部加载更多组件
                        itemBuilder: (context, index) {
                          if (index < controller.userList.length) {
                            return _buildUserItemRow(
                              controller.userList[index],
                            );
                          } else {
                            return PaginationControls(
                              currentPage: controller.currentPage.value,
                              totalPages: controller.totalPages.value,
                              onPageChanged: controller.changePage,
                            );
                          }
                        },
                      ),
                    )
                  : Center(child: Text("暂无数据"));
            }),
          ],
        ),
      ),
    );
  }
}
